package com.xhai.common.domain.login;

import com.alibaba.fastjson2.annotation.JSONField;
import com.xhai.common.constant.DataConstants;
import com.xhai.common.domain.entity.SysDept;
import com.xhai.common.domain.entity.SysPost;
import com.xhai.common.domain.entity.SysUser;
import com.xhai.common.utils.classes.ObjectUtils;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.Collection;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @author xhai
 * @description 登录用户身份权限
 * @date 2025/6/8
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
@Schema(description = "登录用户身份权限")
public class LoginUser implements UserDetails {

    /**
     * 用户信息
     */
    @Schema(description = "用户信息")
    private SysUser user;

    /**
     * 部门信息
     */
    @Schema(description = "部门信息")
    private List<SysDept> depts;

    /**
     * 岗位信息
     */
    @Schema(description = "岗位信息")
    private List<SysPost> posts;

    /**
     * 权限信息
     */
    @Schema(description = "权限信息")
    private Set<String> permissions;

    /**
     * 存储SpringSecurity所需要的权限信息的集合
     */
    @Schema(description = "权限信息的集合")
    private Set<GrantedAuthority> authorities;

    /********登录信息*********/
    /**
     * token
     */
    @Schema(description = "token")
    private String token;

    /**
     * 登录时间
     */
    @Schema(description = "登录时间")
    private Long loginTime;

    /**
     * 过期时间
     */
    @Schema(description = "过期时间")
    private Long expireTime;

    /**********代理***********/
    /**
     * 登录IP地址
     */
    @Schema(description = "登录IP地址")
    private String loginIP;

    /**
     * 登录地点
     */
    @Schema(description = "登录地点")
    private String loginLocation;

    /**
     * 浏览器类型
     */
    @Schema(description = "浏览器类型")
    private String browser;

    /**
     * 操作系统
     */
    @Schema(description = "操作系统")
    private String os;


    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        if (authorities != null) {
            return authorities;
        }
        authorities = permissions.stream().map(SimpleGrantedAuthority::new).collect(Collectors.toSet());
        return authorities;
    }

    @JSONField(serialize = false)
    @Override
    public String getPassword() {
        return user.getPassword();
    }

    @Override
    public String getUsername() {
        return user.getUsername();
    }

    /**
     * 账户是否未过期,过期无法验证
     */
    @JSONField(serialize = false)
    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    /**
     * 指定用户是否解锁,锁定的用户无法进行身份验证
     */
    @JSONField(serialize = false)
    @Override
    public boolean isAccountNonLocked() {
        return ObjectUtils.equals(DataConstants.STATUS_NORMAL, user.getStatus());
    }

    /**
     * 指示是否已过期的用户的凭据(密码),过期的凭据防止认证
     */
    @JSONField(serialize = false)
    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    /**
     * 是否可用 ,禁用的用户不能身份验证
     */
    @JSONField(serialize = false)
    @Override
    public boolean isEnabled() {
        return ObjectUtils.equals(DataConstants.STATUS_NORMAL, user.getStatus());
    }
}
